﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.SqlClient;
using Entities;


namespace DataAccessLayer
{
    public class ThamSoDAL : DBConnection
    {
        public List<ThamSoEntity> getAll()
        {
            List<ThamSoEntity> lst = new List<ThamSoEntity>();
            try
            {
                MySqlCommand cmd = new MySqlCommand("tham_so_getAll", con);
                cmd.CommandType = CommandType.StoredProcedure;

                if (con.State != ConnectionState.Open)
                    con.Open();
                MySqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                while (dr.Read())
                {
                    ThamSoEntity obj = new ThamSoEntity();
                    obj.TenThamSo = Convert.ToString(dr["TenThamSo"]);
                    obj.GiaTriThamSo = Convert.ToDouble(dr["GiaTriThamSo"]);
                    obj.MoTa = Convert.ToString(dr["MoTa"]);
                    lst.Add(obj);
                }
                dr.Close();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
            return lst;
        }

        public ThamSoEntity getByTenThamSo(string _name)
        {
            ThamSoEntity obj = new ThamSoEntity();
            try
            {
                MySqlCommand cmd = new MySqlCommand("tham_so_getByTenThamSo", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("tenThamSo", _name);

                if (con.State != ConnectionState.Open)
                    con.Open();
                MySqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                if (dr.Read())
                {
                    obj.TenThamSo = Convert.ToString(dr["TenThamSo"]);
                    obj.GiaTriThamSo = Convert.ToDouble(dr["GiaTriThamSo"]);
                    obj.MoTa = Convert.ToString(dr["MoTa"]);
                }
                dr.Close();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
            return obj;
        }

        public int modifyThamSo(ThamSoEntity _obj)
        {
            int result = 0;
            try
            {
                MySqlCommand cmd = new MySqlCommand("tham_so_modify", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("tenThamSo", _obj.TenThamSo);
                cmd.Parameters.AddWithValue("giaTri", _obj.GiaTriThamSo);
                cmd.Parameters.AddWithValue("moTa", _obj.MoTa);

                if (con.State != ConnectionState.Open)
                    con.Open();
                result = cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
            return result;
        }
    }
}
